<template>
    <el-select
        v-model="dtoValue"
        v-bind="schema"
        class="select"
    >
        <el-option
            v-for="(item, index) in enumList"
            :key="index"
            :label="item.label"
            :value="item.value"
        ></el-option>
    </el-select>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import $curl from '$aegisCommon/curl.js';

const { schemaKey,schema }= defineProps({
    schemaKey: String,
    schema: Object
})
const emit = defineEmits([ 'loaded' ]);

const dtoValue = ref()
const getValue = ()=>{
    return dtoValue.value !== undefined ? {
        [schemaKey]: dtoValue.value
} : {}
}

const reset = () => {
    dtoValue.value = schema?.option?.default ?? enumList.value[0]?.value;
}

const enumList = ref([])
const fetchEnumList = async () => { 
    const res = await $curl({
        method: 'get',
        url: schema.option?.api,
        errorMessages: '获取枚举列表失败',
        data: {}
    })
    if (res && res.success && res.data.length > 0) {
        enumList.value.push(...res.data)
    }
}

onMounted(() => {
    fetchEnumList()
    reset()
    emit('loaded')
})

defineExpose({
    getValue,
    reset
})
</script>
<style lang="less" scoped></style>